import Vue from 'vue'
import App from './App'
import empty from "./components/empty.vue"
import store from '@/store'
import cuCustom from './colorui/components/cu-custom.vue'
Vue.component('cu-custom', cuCustom);

Vue.config.productionTip = false
Vue.prototype.$store = store

Vue.component('empty', empty);


Vue.prototype.api = "http://47.56.133.189:8188"
Vue.prototype.wapi = "wss://47.56.133.189:8188"


// Vue.prototype.contains =function (str, substr) {
//     return str.indexOf(substr) >= 0;
// }

Vue.prototype.SpotStrategy = [{
	id: 0,
	active: true,
	name: "铁三角",
	isOpen: true
}, 
{
	id: 1,
	active: false,
	name: "步步为赢(公测中)",
	isOpen: false
}, {
	id: 2,
	active: false,
	name: "圣诞树(公测中)",
	isOpen: false
}
, {
	id: 101,
	active: false,
	name: "限价委托(Okex)",
	isOpen: true
}, {
	id: 102,
	active: false,
	name: "止盈止损(Okex)",
	isOpen: true
}, {
	id: 103,
	active: false,
	name: "跟踪委托(Okex)",
	isOpen: true
},
]

Vue.prototype.ContractStrategy = [{
		id: 0,
		active: true,
		name: "铁三角",
		supportClose: true,
	    isOpen: true
	}
	, {
		id: 1,
		active: false,
		name: "宝塔策略(公测中)",
		supportClose: false,		
		isOpen: false
	}, {
		id: 2,
		active: false,
		name: "截血策略(公测中)",
		supportClose: false,
	    isOpen: false
	},
	{
		id:3,
		active: false,
		name: "夹板策略(公测中)",
	    isOpen: false
	}, 
	{
		id: 4,
		active: false,
		name: "防御尖刀(公测中)",
		supportClose: false,
	    isOpen: false
	},
	{
		id: 5,
		active: false,
		name: "止损尖刀(公测中)",
		supportClose: false,
	    isOpen: false
	},
	// {
	// 	id:6,
	// 	active: false,
	// 	name: "步步为赢(公测中)",
	// 	supportClose:true,
	//     isOpen: false
	// },
	// {
	// 	id:7,
	// 	active: false,
	// 	name: "动态遗珠(公测中)",
	// 	supportClose:true,
	//     isOpen: false
	// },
	{
		id: 101,
		active: false,
		name: "限价委托(Okex)",
		supportClose: true,
		isOpen: true
	}, {
		id: 102,
		active: false,
		name: "止盈止损(Okex)",
		supportClose: true,
		isOpen: true
	}, {
		id: 103,
		active: false,
		name: "跟踪委托(Okex)",
		supportClose: true,
		isOpen: true
	}
]

Vue.prototype._PriceRangeGroup = {
	active: "button-wrap-active",
	inactive: "button-wrap",
	btns: [{
			active: false,
			name: "跨度1%",
			value: 1
		},
		{
			active: false,
			name: "跨度2%",
			value: 2
		},
		{
			active: false,
			name: "跨度3%",
			value: 3
		},
		{
			active: false,
			name: "跨度4%",
			value: 4
		},
	]
}
Vue.prototype._MountRangeGroup = {
	active: "button-wrap-active",
	inactive: "button-wrap",
	btns: [{
			active: false,
			name: "10%",
			value: 10
		},
		{
			active: false,
			name: "20%",
			value: 20
		},
		{
			active: false,
			name: "50%",
			value: 50
		},
		{
			active: false,
			name: "100%",
			value: 100
		}
	]
}
Vue.prototype.t2d = function(value) { //04-03 10:53 
	var datetime = new Date();
	datetime.setTime(value > 9999999999 ? value : value * 1000)
	var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
	var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
	var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
	var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
	var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
	var result = month + '-' + date + ' ' + hour + ":" + minute
	return result
}

Vue.prototype.t2dm = function(value) { //04-03 10:53 
	var datetime = new Date();
	datetime.setTime(value > 9999999999 ? value : value * 1000)
	var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
	var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
	var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
	var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
	var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
	var result = hour + ":" + minute
	return result
}

Vue.prototype.assertNotEmpty = function(para, msg) {
	if (para != null && para != "") {
		return true;
	} else {
		uni.showToast({
			title: msg,
			duration: 2000,
			complete() {
				return false
			}
		});
	}
}

Vue.prototype.assertLengthGt = function(para,limit, msg) {
	if (para != null && para != "") {
		if (para.length < limit) {
			uni.showToast({
				icon: 'none',
				title: msg
			});
			return false;
		}else{
			return true;
		}		
	} else {
		uni.showToast({
			title: msg,
			duration: 2000,
			complete() {
				return false
			}
		});
	}
}

Vue.prototype.assertBigThanZero = function(para, msg) {
	if (para != null && Number(para) > 0) {
		return true;
	} else {
		uni.showToast({
			title: msg,
			duration: 2000,
			complete() {
				return false
			}
		});
	}
}

Vue.prototype.assertTrue = function(para, msg) {
	if (para != null && para) {
		return true;
	} else {
		uni.showToast({
			title: msg,
			duration: 2000,
			complete() {
				return false
			}
		});
	}
}

Vue.prototype.checkLogin2 = function() {
	var token = uni.getStorageSync('XAUTHTOKEN');
	if (token == '') {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	}
	console.log('check login is true ')
	return true;
}

Vue.prototype.checkLogin = function() {
	var token = uni.getStorageSync('XAUTHTOKEN');
	if (token == '') {
		console.log('check login is false')
		return false;
	}
	console.log('check login is true ')
	return true;
}

Vue.prototype.ifElseReturn = function(res) {
	console.log("ifElseReturn and status code is " + res.statusCode)
	uni.hideLoading()
	if (res.data.code > 200000) {
		if(res.data.msg!=''){
			uni.showToast({
				title: res.data.msg,
				duration: 2000
			});
		}else{
			uni.showToast({
				title: '服务不可用，稍后再试',
				duration: 2000
			});
		}
		return false;
	} 
	return true	
}

Vue.prototype.getToken = function() {
	return uni.getStorageSync('XAUTHTOKEN');
}

Vue.prototype.removeToken = function() {
	return uni.removeStorageSync('XAUTHTOKEN');
}


//******get set markets*********//
Vue.prototype.getMarkets = function() {
	return uni.getStorageSync('CONTRACTS_MARKETS');
}
Vue.prototype.cacheMarkets = function(markets) {
	uni.setStorageSync('CONTRACTS_MARKETS', markets);
}
//******get set default contract*********//
Vue.prototype.getDefaultContract = function() {
	return uni.getStorageSync('DEFAULT_CONTRACT_INFO');
}

Vue.prototype.cacheDefaultContract = function(contract) {
	uni.setStorageSync('DEFAULT_CONTRACT_INFO', contract);
}

//******get set default spot intrument*********//
Vue.prototype.getDefaultSpot = function() {
	return uni.getStorageSync('DEFAULT_INSTRUMENT_INFO');
}

Vue.prototype.cacheDefaultSpot = function(instrument) {
	uni.setStorageSync('DEFAULT_INSTRUMENT_INFO', instrument);
}
//******get set default spot Markets*********//
Vue.prototype.getSpotMarket = function() {
	return uni.getStorageSync('DEFAULT_SPOT_MARKET');
}

Vue.prototype.cacheSpotMarket = function(markets) {
	uni.setStorageSync('DEFAULT_SPOT_MARKET', markets);
}

//******get set default spot Markets*********//
Vue.prototype.getAccount = function() {
	return uni.getStorageSync('DEFAULT_ACCOUNT');
}

Vue.prototype.cacheAccount = function(account) {
	uni.setStorageSync('DEFAULT_ACCOUNT', account);
}
//******get set contract map*********//
Vue.prototype.getContractMap = function() {
	var map = uni.getStorageSync('K_CONTRACTS_MAP')
	let strMap = new Map();
	if (map != null && map.length > 0) {
		for (let k of map) {
			strMap.set(k.instrument_id, k);
		}
	}

	return strMap;
}

Vue.prototype.cacheContractMap = function(map) {
	uni.setStorageSync('K_CONTRACTS_MAP', map);
}
//*************group method start*******************//
Vue.prototype.changeGroup = function(index, group) {
	for (let btnIndex in group.btns) {
		if (index == btnIndex) {
			group.btns[btnIndex].active = !group.btns[btnIndex].active
		} else {
			group.btns[btnIndex].active = false
		}
	}
}
Vue.prototype.changeGroup2 = function(index, group) {
	for (let btnIndex in group.btns) {
		if (index == btnIndex) {
			group.btns[btnIndex].active = true
		} else {
			group.btns[btnIndex].active = false
		}
	}
}
Vue.prototype.cleanGroup = function(group) {
	for (let item of group.btns) {
		item.active = false
	}
}
Vue.prototype.hasActive = function(group) {
	for (let item of group.btns) {
		if (item.active) return true
	}
	return false
}

Vue.prototype.getActive = function(group) {
	for (let item of group) {
		if (item.active) return item
	}
}

//*************group method end*******************//

//************** web socket status start*********************//
Vue.prototype.setWSStatus = function(status) {
	uni.setStorageSync('WEBSOCKET_STATUS', status);
}

Vue.prototype.getWSStatus = function() {
	return uni.getStorageSync('WEBSOCKET_STATUS');
}

Vue.prototype.cleanWSStatus = function() {
	return uni.setStorageSync('WEBSOCKET_STATUS', null);
}

Vue.prototype.wsConnect = function(socketToken) {
	var _self = this
	if (_self.getWSStatus() != 'connectSocket' && _self.getWSStatus() != 'onSocketOpen') {
		console.log('connnect socket and token is ' + socketToken)
		console.log(_self.getWSStatus())
		uni.connectSocket({
			url: this.wapi + '/imserver/' + socketToken, //仅为示例，并非真实接口地址。		
			success() {
				_self.setWSStatus('connectSocket')
			},
			fail() {
				_self.setWSStatus('connectFail')
			}
		});
	}


	uni.onSocketOpen(function(res) {
		console.log("链接建立")
		_self.setWSStatus('onSocketOpen')
	});

	uni.onSocketError(function(res) {
		console.log('WebSocket连接打开失败，请检查！');
		_self.setWSStatus('onSocketError')
	});

	uni.onSocketClose(function(res) {
		console.log("关闭链接")
		_self.setWSStatus('onSocketClose')
	});

}

//************** web socket status end*********************//

//*********** set share url start ***************//
Vue.prototype.setShareUrl = function(url) {
	uni.setStorageSync('USER_INFO_SHARE_URL', url);
}

Vue.prototype.getShareUrl = function() {
	return uni.getStorageSync('USER_INFO_SHARE_URL');
}
//*********** set share url end ***************//
//*********** set user channel start ***************//
Vue.prototype.setChannel = function(channel) {
	uni.setStorageSync('USER_CHANNEL', channel);
}

Vue.prototype.getChannel = function() {
	return uni.getStorageSync('USER_CHANNEL');
}
//*********** set get user channel end ***************//

App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()
